package com.tenton.controller;

import com.tenton.MyException.DataNotExist;
import com.tenton.pojo.Stock;
import com.tenton.service.CommodityService;
import com.tenton.service.StockService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Date: 2021/1/9
 * @Author: Tenton
 * @Description:
 */
@ApiIgnore
@Controller
@ApiOperation("管理员仓库管理")
@RequestMapping("/admin/flatform/stock")
public class AStockController {
    @Autowired
    StockService stockService;
    @Autowired
    CommodityService commodityService;

    /**
     * 跳转到增加页面
     *
     * @return
     */
    @ApiOperation("跳转到增加页面")
    @GetMapping("/toAdd")
    public String toAdd() {
        return "admin/stock/addStock";

    }

    /**
     * 删除仓库
     *
     * @param id
     * @return
     */
    @ApiOperation("通过Id删除仓库")
    @GetMapping("/delete")
    public String delete(int id) {
        stockService.deleteByPrimaryKey(id);
        return "redirect:/admin/flatform/stock/getAll";
    }

    /**
     * 跳转到修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @ApiOperation("跳转到修改页面")
    @GetMapping("/toUpdate")
    public String editStock(int id, Model model) {
        stockService.selectByPrimaryKey(id, model);
        return "admin/stock/editStock";
    }

    /**
     * 修改仓库信息
     *
     * @param stock
     * @return
     */
    @ApiOperation("修改仓库信息")
    @PostMapping("/update")
    public String update(Stock stock, HttpSession session, HttpServletRequest request) {
        try {
            stockService.update(stock,session);
            return "redirect:/admin/flatform/stock/getAll";
        } catch (DataNotExist dataNotExist) {
            request.setAttribute("msg", "仓库信息为空！");
            return "admin/stock/editStock";
        }
    }

    /**
     * 获取所有仓库信息
     *
     * @param model
     * @param pn
     * @return
     */
    @ApiOperation("获取所有仓库信息")
    @GetMapping("/getAll")
    public String getAll_stock(ModelMap model,
                               @ApiParam("页码") @RequestParam(defaultValue = "1", required = true, value = "pn") Integer pn
    ) {
        stockService.getAll(pn, model);
        return "admin/stock/getStockList";

    }

    /**
     * 查询单个仓库信息
     *
     * @param id
     * @param model
     * @return
     */
    @ApiOperation("通过Id查询单个仓库信息")
    @PostMapping("/getStock")
    public String getStock(int id, Model model) {
        stockService.getStock(id, model);
        return "admin/stock/getStock";
    }

    /**
     * 仓库关联商品
     *
     * @param comId
     * @param model
     * @return
     */
    @ApiOperation("仓库关联商品")
    @GetMapping("/StockWithCom")
    public String StockWithCom(Integer comId, Model model) {
        stockService.StockWithCom(comId, model);
        return "admin/stock/promotion";
    }

    /**
     * 库存预警
     *
     * @param model
     * @param pn
     * @return
     */
    @ApiOperation("库存预警")
    @GetMapping("/stockWithComNum")
    public String stockWithComNum(ModelMap model,
                                  @ApiParam("页码") @RequestParam(defaultValue = "1", required = true, value = "pn") Integer pn
    ) {
        stockService.StockWithComNum(pn, model);
        return "admin/stock/getStockNum";
    }

    /**
     * 临期产品
     *
     * @param model
     * @param pn
     * @return
     */
    @ApiOperation("临期产品")
    @GetMapping("/stockWithComData")
    public String stockWithComData(ModelMap model,
                                   @ApiParam("页码") @RequestParam(defaultValue = "1", required = true, value = "pn") Integer pn
    ) {
        stockService.StockWithComData(pn, model);
        return "admin/stock/getStockData";
    }

    /**
     * 日期格式化
     *
     * @param binder
     */
    @InitBinder
    protected void init(ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

    /**
     * 商品名称模糊查询
     *
     * @param model
     * @param comName
     * @param pn
     * @return
     */
    @ApiOperation("商品名称模糊查询")
    @GetMapping("/getByParams")
    public String getByParams(ModelMap model,
                              @ApiParam("商品名称") @RequestParam(value = "comName", required = false) String comName,
                              @ApiParam("页码") @RequestParam(defaultValue = "1", required = true, value = "pn") Integer pn
    ) {
        stockService.getByParams(comName, pn, model);
        return "admin/stock/getStockByParams";

    }

    /**
     * 查询仓库信息
     *
     * @param comId
     * @return
     */
    @ApiOperation("通过Id查询仓库信息")
    @PostMapping("/getStocks")
    public Stock getStocks(int comId) {
        return stockService.stockWithComId(comId);
    }
}
